<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function add(...args) {
            let arr = [...args]
            function fn(...newArgs) {
                arr = [...arr, ...newArgs]
                return fn
            }
            
            fn.toString = fn.valueOf = function() {
                return arr.reduce((pre, cur) => pre + cur)
            }

            return fn
        }
        console.log(add(1)); // 1
        console.log(add(1)(2)); // 3
        console.log(add(1, 2)(3, 4, 5)(6)); // 21
    </script>
</body>
</html>